/**
 *  Copyright (c) Filipe Manana <fdmanana@gmail.com>
 *  All rights reserved.
 * 
 *  This file is part of jsdet.
 *
 *  jsdet is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published
 *  by the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  jsdet is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with jsdet.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


package jsdet.tests;


import jsdet.exceptions.ParsingException;
import jsdet.parsing.Parser;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;


/**
 * 
 * @author Filipe Manana
 *
 */
public class JavaScriptRecognitionTest extends TestCase {


    public static Test suite() {
        TestSuite suite = new TestSuite();

        suite.addTest(new JavaScriptRecognitionTest("testGoodSimple"));
        suite.addTest(new JavaScriptRecognitionTest("testBadSimple"));
        suite.addTest(new JavaScriptRecognitionTest("testGoodFunctions"));
        suite.addTest(new JavaScriptRecognitionTest("testBadFunctions"));
        suite.addTest(new JavaScriptRecognitionTest("testGoodVarDeclarations"));
        suite.addTest(new JavaScriptRecognitionTest("testBadVarDeclarations"));
        suite.addTest(new JavaScriptRecognitionTest("testGoodExpressionStatements"));
        suite.addTest(new JavaScriptRecognitionTest("testBadExpressionStatements"));
        suite.addTest(new JavaScriptRecognitionTest("testGoodConditionalStatements"));
        suite.addTest(new JavaScriptRecognitionTest("testBadConditionalStatements"));
        suite.addTest(new JavaScriptRecognitionTest("testGoodLoopStatements"));
        suite.addTest(new JavaScriptRecognitionTest("testBadLoopStatements"));
        suite.addTest(new JavaScriptRecognitionTest("testGoodEverything"));

        return suite;
    }


    public JavaScriptRecognitionTest(String testName) {
        super(testName);
    }


    @Override
    protected void setUp() throws Exception {
    }


    @Override
    protected void tearDown() throws Exception {
    }


    public void testGoodSimple() throws Exception {
        Parser parser = new Parser(TestsSettings.INPUT_FILES_DIR + "good_simple.js");

        parser.doParsing();

        assertFalse(parser.hasErrors());
        assertTrue(parser.getErrors() == null);
    }


    public void testBadSimple() throws Exception {
        Parser parser = new Parser(TestsSettings.INPUT_FILES_DIR + "bad_simple.js");
        boolean parseException = false;

        try {
            parser.doParsing();
        }
        catch (ParsingException ex) {
            parseException = true;
        }

        assertTrue(parseException);
        assertTrue(parser.hasErrors());
        //assertTrue(parser.getErrors() != null);
        //assertTrue(parser.getErrors().size() > 0);
    }


    public void testGoodFunctions() throws Exception {
        Parser parser = new Parser(TestsSettings.INPUT_FILES_DIR + "good_functions.js");

        parser.doParsing();

        assertFalse(parser.hasErrors());
        assertTrue(parser.getErrors() == null);
    }


    public void testBadFunctions() throws Exception {
        Parser parser = new Parser(TestsSettings.INPUT_FILES_DIR + "bad_functions.js");
        boolean parseException = false;

        try {
            parser.doParsing();
        }
        catch (ParsingException ex) {
            parseException = true;
        }

        assertTrue(parseException);
        assertTrue(parser.hasErrors());
        //assertTrue(parser.getErrors() != null);
        //assertTrue(parser.getErrors().size() > 0);
    }


    public void testGoodVarDeclarations() throws Exception {
        Parser parser = new Parser(TestsSettings.INPUT_FILES_DIR + "good_vardecls.js");

        parser.doParsing();

        assertFalse(parser.hasErrors());
        assertTrue(parser.getErrors() == null);
    }


    public void testBadVarDeclarations() throws Exception {
        Parser parser = new Parser(TestsSettings.INPUT_FILES_DIR + "bad_vardecls.js");
        boolean parseException = false;

        try {
            parser.doParsing();
        }
        catch (ParsingException ex) {
            parseException = true;
        }

        assertTrue(parseException);
        assertTrue(parser.hasErrors());
        //assertTrue(parser.getErrors() != null);
        //assertTrue(parser.getErrors().size() > 0);
    }


    public void testGoodExpressionStatements() throws Exception {
        Parser parser = new Parser(TestsSettings.INPUT_FILES_DIR + "good_exprStatements.js");

        parser.doParsing();

        assertFalse(parser.hasErrors());
        assertTrue(parser.getErrors() == null);
    }


    public void testBadExpressionStatements() throws Exception {
        Parser parser = new Parser(TestsSettings.INPUT_FILES_DIR + "bad_exprStatements.js");
        boolean parseException = false;

        try {
            parser.doParsing();
        }
        catch (ParsingException ex) {
            parseException = true;
        }

        assertTrue(parseException);
        assertTrue(parser.hasErrors());
        //assertTrue(parser.getErrors() != null);
        //assertTrue(parser.getErrors().size() > 0);
    }


    public void testGoodConditionalStatements() throws Exception {
        Parser parser = new Parser(TestsSettings.INPUT_FILES_DIR + "good_conditionals.js");

        parser.doParsing();

        assertFalse(parser.hasErrors());
        assertTrue(parser.getErrors() == null);
    }


    public void testBadConditionalStatements() throws Exception {
        Parser parser = new Parser(TestsSettings.INPUT_FILES_DIR + "bad_conditionals.js");
        boolean parseException = false;

        try {
            parser.doParsing();
        }
        catch (ParsingException ex) {
            parseException = true;
        }

        assertTrue(parseException);
        assertTrue(parser.hasErrors());
        //assertTrue(parser.getErrors() != null);
        //assertTrue(parser.getErrors().size() > 0);
    }


    public void testGoodLoopStatements() throws Exception {
        Parser parser = new Parser(TestsSettings.INPUT_FILES_DIR + "good_loops.js");

        parser.doParsing();

        assertFalse(parser.hasErrors());
        assertTrue(parser.getErrors() == null);
    }


    public void testBadLoopStatements() throws Exception {
        Parser parser = new Parser(TestsSettings.INPUT_FILES_DIR + "bad_loops.js");
        boolean parseException = false;

        try {
            parser.doParsing();
        }
        catch (ParsingException ex) {
            parseException = true;
        }

        assertTrue(parseException);
        assertTrue(parser.hasErrors());
        //assertTrue(parser.getErrors() != null);
        //assertTrue(parser.getErrors().size() > 0);
    }


    public void testGoodEverything() throws Exception {
        Parser parser = new Parser(TestsSettings.INPUT_FILES_DIR + "good_everything.js");

        parser.doParsing();

        assertFalse(parser.hasErrors());
        assertTrue(parser.getErrors() == null);
    }
}
